<?php
namespace PingNa\Controller;
use Common\Lib\Category;
use Think\Controller;
class PostController extends HomeCommonController {
    public function index(){
        //获取选择栏目信息
        $catalogId = I('catalogId');
        $catalogTop = Category::getTopCatalog($catalogId); //获取顶级栏目信息
        $catalogInfo  = M('category')->find($catalogId);//获取栏目信息 $catalogInfo
		if(!is_array($catalogInfo)){
            $catalogName = I('catalogName');
            $catalogInfo =  M('category')->where(['ename'=>$catalogName])->find();//获取栏目信息 $catalogInfo
            $catalogTop = Category::getTopCatalog($catalogInfo['id']); //获取顶级栏目信息
        }
        $this->assign('avtiveId',$catalogTop['id']);
        $this->assign('headInfo',$catalogTop);
        $this->assign('catalogInfo',$catalogInfo);
        //print_r($catalogTop);exit;
        $headHtml = $this->getTemplatePage('header.html');
        $footHtml = $this->getTemplatePage('foot.html');
        $head_navlist = $this->getTemplatePage('head_navlist.html');

        $tpl = getCatalogTemple($catalogTop['id']);
        switch($tpl){
            case 'site': //首页执行301跳转
                Header("HTTP/1.1 301 Moved Permanently");
                Header("Location: ".'http://'.$_SERVER['HTTP_HOST']);
                exit;
                break;
            case 'aboutUs': //企业简介
                if($catalogTop['id']==$catalogInfo['id']){
                    $activeCatalogId = 152;
                    $this->assign('activeCatalogId',$activeCatalogId);
                }else{
                    $activeCatalogId = $catalogInfo['id'];
                    $this->assign('activeCatalogId',$catalogInfo['id']);
                    switch($catalogInfo['modelid']){
                        case 2:
                            $tpl = 'page';
                            break;
                        case 4:
                            $tpl = getCatalogTemple($activeCatalogId);
                            break;
                        default:
                            $tpl = 'page';
                            break;
                    }
                }

                $catalogSelect = M('category')->find($activeCatalogId);
                $this->assign('catalogSelect',$catalogSelect);
                break;
            case 'brand': //产品系列
                $activeCatalogId = 141; //默认选择
                $catalogSelect = M('category')->find($activeCatalogId);
                $this->assign('catalogSelect',$catalogSelect);
                break;
            case 'Join': //加盟
                if($catalogTop['id']==$catalogInfo['id']){
                    $activeCatalogId = 142;
                }else{
                    $activeCatalogId = $catalogInfo['id'];
                }
                $this->assign('activeCatalogId',$activeCatalogId);
                $tpl = getCatalogTemple($activeCatalogId);
                $catalogSelect = M('category')->find($activeCatalogId);
                $this->assign('catalogSelect',$catalogSelect);
                break;
            case 'News': //新闻中心
                    $tpl = getCatalogTemple($catalogTop['id']);
                    $activeCatalogId = $catalogInfo['id'];
                    $this->assign('activeCatalogId',$activeCatalogId);
                    $catalogSelect = M('category')->find($activeCatalogId);
                    $this->assign('catalogSelect',$catalogSelect);

                    $selectPage = I('selectPage');
                    if(empty($selectPage)){
                        $selectPage = 0;
                    }
                    $pageCount = 6;
                    if($catalogSelect['id']==$catalogTop['id']){
                        $ids = M('category')->field('id')->where(['pid'=>$catalogSelect['id']])->select();
                        $idArray = 'cid in (';
                        foreach($ids as $key => $d){
                            if($key>0) {
                                $idArray.=',';
                            }
                            $idArray .= $d['id'];
                        }
                        $idArray.=')';
                        $idArray.=' and status=0';
                        $newsCount = M('article')->where($idArray)->count(); //总数量
                    }else {
                        $newsCount = M('article')->where(['cid' => $catalogSelect['id'],'status'=>0])->count(); //总数量
                    }

                    $this->assign('pageCount',$pageCount);
                    $this->assign('newsCount',$newsCount);
                    $this->assign('selectPage',$selectPage);

                break;
            case 'Link': //旗下品牌
                $activeCatalogId = 173; //默认选择
                $catalogSelect = M('category')->find($activeCatalogId);
                $this->assign('catalogSelect',$catalogSelect);
                $mapInfo = M('map')->select();
                $this->assign('mapInfo',$mapInfo);
                break;
        }

        $this->assign('headHtml',$headHtml);
        $this->assign('footHtml',$footHtml);
        $this->assign('head_navlist',$head_navlist);
        $this->display($tpl);


    }
}?>